package com.device.api.job;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.device.api.entity.PudongDeviceConfig;
import com.device.api.mapper.PudongDeviceConfigMapper;
import com.device.api.service.MqttService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

@Component
@Slf4j
public class SchedulerQz {
    @Autowired
    private MqttService mqttService;

    @Autowired
    private PudongDeviceConfigMapper pudongDeviceConfigMapper;

    @Scheduled(cron = "0 0 1 * * ?") // 每天凌晨1点执行一次
//    @Scheduled(cron = "0 * * * * *")
    public void task() {

        JSONObject bodyJson = new JSONObject();
        JSONObject payload = new JSONObject();
        JSONObject content = new JSONObject();
        JSONObject cmd = new JSONObject();
        // 定义日期格式化对象
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 取当前时间
        Date date = new Date();
        // 格式化当前时间
        String nowTime = simpleDateFormat.format(date);

        content.set("datetime",nowTime);
        // 服务参数
        cmd.set("set_datetime",content);
        payload.set("payload",cmd);


        bodyJson.set("content",payload);

        List<PudongDeviceConfig> pudongDeviceConfigs = pudongDeviceConfigMapper.selectList(new QueryWrapper<>());
        for (PudongDeviceConfig pudongDeviceConfig : pudongDeviceConfigs) {
            mqttService.sendMessage(pudongDeviceConfig,String.valueOf(cmd));
        }
//        mqttService.sendAllMessage(String.valueOf(cmd));
        log.info("进行了一次设备的时队");
    }

}
